﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;
using System.Data.SqlClient;
using TimKen.DataEntity;

namespace TimKen.DataAccess
{
    public class UserAccountDAO
    {
        public UserAccountDAO()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string UserName)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50)};
            parameters[0].Value = UserName;

            int result = DbHelperSQL.RunProcedure("UP_UserAccount_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        //public User  Login(string UserName,string Password)
        //{
        //    int rowsAffected;
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@UserName", SqlDbType.VarChar,50),
        //            new SqlParameter("@Password",SqlDbType.VarChar,50),
        //           new SqlParameter("@Result",SqlDbType.Int)          
        //                                };

        //    parameters[0].Value = UserName;
        //    parameters[1].Value = Password;
        //    parameters[2].Direction = ParameterDirection.Output;

        //    DbHelperSQL.RunProcedure("UP_User_Login", parameters, out rowsAffected);

        //    if (Convert.ToInt32(parameters[2].Value.ToString()) == 1)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public void Add(TimKen.DataEntity.UserAccount model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,32),
					new SqlParameter("@Password", SqlDbType.VarChar,50),
					new SqlParameter("@RealName", SqlDbType.VarChar,20),
					new SqlParameter("@CustomerID", SqlDbType.Int,4),
					new SqlParameter("@Phone", SqlDbType.VarChar,15),
					new SqlParameter("@Email", SqlDbType.VarChar,200),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@CreatorID", SqlDbType.Int,4),
					new SqlParameter("@RoleID", SqlDbType.Int,4),
                    new SqlParameter("@LastUpdatePwd",SqlDbType.DateTime)};
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.Password;
            parameters[2].Value = model.RealName;
            parameters[3].Value = model.CustomerID;
            parameters[4].Value = model.Phone;
            parameters[5].Value = model.Email;
            parameters[6].Value = model.CreateDate;
            parameters[7].Value = model.CreatorID;
            parameters[8].Value = model.RoleID;
             parameters[9].Value = model.LastUpdatePwd;

            DbHelperSQL.RunProcedure("UP_UserAccount_ADD", parameters, out rowsAffected);
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.UserAccount model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,32),
					new SqlParameter("@Password", SqlDbType.VarChar,50),
					new SqlParameter("@RealName", SqlDbType.VarChar,20),
					new SqlParameter("@CustomerID", SqlDbType.Int,4),
					new SqlParameter("@Phone", SqlDbType.VarChar,15),
					new SqlParameter("@Email", SqlDbType.VarChar,200),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@CreatorID", SqlDbType.Int,4),
					new SqlParameter("@RoleID", SqlDbType.Int,4),
                    new SqlParameter("@LastUpdatePwd",SqlDbType.DateTime),
                    new SqlParameter("@Points", SqlDbType.Int,4),
                  };
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.Password;
            parameters[2].Value = model.RealName;
            parameters[3].Value = model.CustomerID;
            parameters[4].Value = model.Phone;
            parameters[5].Value = model.Email;
            parameters[6].Value = model.CreateDate;
            parameters[7].Value = model.CreatorID;
            parameters[8].Value = model.RoleID;
            parameters[9].Value = model.LastUpdatePwd;
            parameters[10].Value = model.Points;
            DbHelperSQL.RunProcedure("UP_UserAccount_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string UserName,string WhoDelete)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,32),
                    new SqlParameter("@WhoDelete",SqlDbType.VarChar,32)
                                        };
            parameters[0].Value = UserName;
            parameters[1].Value = WhoDelete;
            DbHelperSQL.RunProcedure("UP_UserAccount_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.UserAccount GetModel(string UserName)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50)};
            parameters[0].Value = UserName;

            TimKen.DataEntity.UserAccount model = new TimKen.DataEntity.UserAccount();
            DataSet ds = DbHelperSQL.RunProcedure("UP_UserAccount_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                model.Password = ds.Tables[0].Rows[0]["Password"].ToString();
                model.RealName = ds.Tables[0].Rows[0]["RealName"].ToString();
                if (ds.Tables[0].Rows[0]["CustomerID"].ToString() != "")
                {
                    model.CustomerID = int.Parse(ds.Tables[0].Rows[0]["CustomerID"].ToString());
                }
                model.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                if (ds.Tables[0].Rows[0]["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreatorID"].ToString() != "")
                {
                    model.CreatorID = int.Parse(ds.Tables[0].Rows[0]["CreatorID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RoleID"].ToString() != "")
                {
                    model.RoleID = int.Parse(ds.Tables[0].Rows[0]["RoleID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LastUpdatePwd"].ToString() != "")
                {
                    model.LastUpdatePwd = DateTime.Parse(ds.Tables[0].Rows[0]["LastUpdatePwd"].ToString());
                }
                model.ActionList = ds.Tables[0].Rows[0]["ActionList"].ToString();
                if (ds.Tables[0].Rows[0]["Points"].ToString() != "") {
                    model.Points = int.Parse(ds.Tables[0].Rows[0]["Points"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserName,Password,RealName,CustomerID,Phone,Email,CreateDate,CreatorID,Role,LastUpdatePwd ");
            strSql.Append(" FROM UserAccount ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetUserAccountPageList(int pagesize, int pageindex, string userType, string customerID, string userName,int languageid)
        {

            SqlParameter[] parameters = {
                  
                  
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@RoleID", SqlDbType.Int),
                    new SqlParameter("@CustomerID", SqlDbType.Int),
                    new SqlParameter("@UserName",SqlDbType.VarChar,50),
                    new SqlParameter("@LanguageID",SqlDbType.Int)
                    };
            parameters[0].Value = pagesize;
            parameters[1].Value = pageindex;
            parameters[2].Value = userType;
            parameters[3].Value = customerID;
            parameters[4].Value = userName;
            parameters[5].Value = languageid;
            return DbHelperSQL.RunProcedure("UP_UserAccount_GetPageList", parameters, "ds");
        }

        #endregion  成员方法


        #region  Role
        /// <summary>
        /// 获取后台管理用户类型
        /// </summary>
        /// <param name="languageid">语言ID</param>
        /// <returns></returns>
        public DataSet GetRoles(int languageid) 
        {
            string sql = "Select * from Role  where LanguageID="+languageid.ToString()+" order by RoleID";//不担心sql注入。系统内部数据
            return DbHelperSQL.Query(sql);
        }

        public UserS Login(string userName, string password) 
        {
            SqlParameter[] parameters =
            {
                new SqlParameter("@UserName",SqlDbType.VarChar,32),
                new SqlParameter("@Password",SqlDbType.VarChar,32)
            };
            parameters[0].Value = userName;
            parameters[1].Value = password;
            UserAccount model = new UserAccount();
            DataSet ds = DbHelperSQL.RunProcedure("UP_UserAccount_Login", parameters,"ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                model.Password = ds.Tables[0].Rows[0]["Password"].ToString();
                model.RealName = ds.Tables[0].Rows[0]["RealName"].ToString();
                if (ds.Tables[0].Rows[0]["CustomerID"].ToString() != "")
                {
                    model.CustomerID = int.Parse(ds.Tables[0].Rows[0]["CustomerID"].ToString());
                }
                model.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                if (ds.Tables[0].Rows[0]["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreatorID"].ToString() != "")
                {
                    model.CreatorID = int.Parse(ds.Tables[0].Rows[0]["CreatorID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RoleID"].ToString() != "")
                {
                    model.RoleID = int.Parse(ds.Tables[0].Rows[0]["RoleID"].ToString());
                }

                if (ds.Tables[0].Rows[0]["LastUpdatePwd"].ToString() != "")
                {
                    model.LastUpdatePwd = DateTime.Parse(ds.Tables[0].Rows[0]["LastUpdatePwd"].ToString());
                }

                model.ActionList = ds.Tables[0].Rows[0]["ActionList"].ToString();
                if (ds.Tables[0].Rows[0]["Points"].ToString() != "") {
                    model.Points = int.Parse(ds.Tables[0].Rows[0]["Points"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }
        #endregion



        /// <summary>
        ///  更新管理员的权限列表
        /// </summary>
        public void Update(string UserName,string ActionList)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,32),
					new SqlParameter("@ActionList",SqlDbType.NVarChar,4000)};
            parameters[0].Value = UserName;
            parameters[1].Value = ActionList;
            DbHelperSQL.RunProcedure("UP_UserAccount_UpdateActionList", parameters, out rowsAffected);
        }


    }
}
